package com.cx.base.day19_thread;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/17
 */
@Slf4j(topic = "Thread_11_volatile优化两阶段")
public class Thread_11_volatile优化两阶段 extends Thread {
    private static volatile boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        optimize();
        sleep(1000);
        toStop();
    }

    public static void optimize() {
        new Thread(() -> {
            while (true) {
                Thread current = Thread.currentThread();
                if (stop) {
                    log.info("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.info("执行监控");
                } catch (InterruptedException e) {
                    current.interrupt();
                }
            }
        }, "monitor").start();
    }

    public static void toStop() {
        stop = true;
    }
}



